/**  
 * All rights Reserved, Designed By www.beonelot.com
 * @Package: com.bccah.modlib.dbdo.service.impl
 * @Filename: AcsControlRoomInfoServiceImpl.java
 * @Description: TODO(AcsControlRoomInfoService接口实现)
 * @author: 金鑫智慧
 * @date: 2020-04-07
 * @version: V1.0
 * @Copyright: 2018 www.beonelot.com Inc. All rights reserved.
 * 注意：本内容仅限于重庆金鑫智慧科技有限公司内部传阅，禁止外泄以及用于其他的商业目的
 */
package com.jinxin.platform.cdfaceacs.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.jinxin.platform.cdfaceacs.common.CommonCodeEntity;
import com.jinxin.platform.cdfaceacs.mapper.JurenAcsControlRoomInfoMapper;
import com.jinxin.platform.cdfaceacs.pojo.domains.AcsControlRoomInfoDO;
import com.jinxin.platform.cdfaceacs.pojo.entity.Constant;
import com.jinxin.platform.cdfaceacs.pojo.vo.ResultVO2;
import com.jinxin.platform.cdfaceacs.service.AcsControlRoomInfoService;
import com.jinxin.platform.cdfaceacs.service.RestTemplateService;
import com.jinxin.platform.cdfaceacs.utils.POIUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO(AcsControlRoomInfoService接口实现)
 * @Classn: AcsControlRoomInfoServiceImpl
 * @author SY
 * @date 2020-04-07
 */
@Service
public class AcsControlRoomInfoServiceImpl  implements AcsControlRoomInfoService {

    @Resource
    JurenAcsControlRoomInfoMapper jurenAcsControlRoomInfoMapper;
    @Autowired
    private RestTemplateService restTemplateService;

    @Override
    public AcsControlRoomInfoDO getById(String roomId) {
        return jurenAcsControlRoomInfoMapper.selectByPrimaryKey(roomId);
    }

    @Override
    public List<Map> selAllRoom() {
        return jurenAcsControlRoomInfoMapper.selAllRoom();
    }

    @Override
    public List<Map> selRoomByBuild(String buildLoc) {
        return jurenAcsControlRoomInfoMapper.selRoomByBuild(buildLoc);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map uploadRoom(MultipartFile file, String community, String housingEstateId)throws Exception {
        Map<String,String > resMap = new HashMap<>();
        List<String[]> products = POIUtil.readExcel(file, 4);
        for (String[] prod : products){
            String[] productInfo = prod;
            if(StringUtils.isEmpty(productInfo[0]) || StringUtils.isEmpty(productInfo[2])){
                continue;
            }
            AcsControlRoomInfoDO infoDO = new AcsControlRoomInfoDO();
            infoDO.setCompanyId(community);
            infoDO.setHousingEstateId(housingEstateId);
            infoDO.setRoomName(productInfo[0]);
            infoDO.setBlockId(productInfo[2]);
            ResultVO2 resultVO = restTemplateService.buildingunitOperate(Constant.OPERATION_SIGN_ADD, infoDO);
            infoDO.setRoomDesc(productInfo[1]);
            infoDO.setBlockName(productInfo[3]);
            infoDO.setCreateTime(LocalDateTime.now());
            infoDO.setState("0");
            if(resultVO != null && resultVO.getRstId().equals(Constant.ETO_TOKEN_RSTID_SUCCESS)){
                infoDO.setRoomId(resultVO.getObject().toString());
                jurenAcsControlRoomInfoMapper.insert(infoDO);
            }
        }
        resMap.put("resultCode", CommonCodeEntity.SUC_CODE);
        resMap.put("resultDesc", "suc");
        return resMap;
    }

    @Override
    public PageInfo<AcsControlRoomInfoDO> selParamPage(int page, int size, String roomName,String community,String blockId, String blockName) {
        PageHelper.startPage(page,size);//这行是重点，表示从pageNum页开始，每页pageSize条数据
        List<AcsControlRoomInfoDO> list = jurenAcsControlRoomInfoMapper.selAllTable(roomName,community,blockId,blockName);
        PageInfo<AcsControlRoomInfoDO> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public int saveDO(AcsControlRoomInfoDO paramDO) {
        return jurenAcsControlRoomInfoMapper.insert(paramDO);
    }

    @Override
    public int updateDO(AcsControlRoomInfoDO paramDO) {
        return jurenAcsControlRoomInfoMapper.updateByPrimaryKeySelective(paramDO);
    }

    @Override
    public int delDO(String pid) {
        return jurenAcsControlRoomInfoMapper.deleteByPrimaryKey(pid);
    }

    @Override
    public List<AcsControlRoomInfoDO> selDOByInfo(AcsControlRoomInfoDO paramDO) {
        return jurenAcsControlRoomInfoMapper.selDOByInfo(paramDO );
    }

}
